{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "authorized-gambling",
   "metadata": {},
   "outputs": [],
   "source": [
    "from model.resnet_sgd_cosineannealing_inference import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "defensive-swift",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define input image dimensions for resizing\n",
    "height = 448\n",
    "width = 448\n",
    "\n",
    "# Define model hyperparameters\n",
    "lr = 0.01\n",
    "momentum = 0.9\n",
    "T_0 = 225 # e.g. 899 / 4 (train_dataset_size / batch_size)\n",
    "T_mult = 1\n",
    "epochs = 10\n",
    "batch_size = 4 # For both train and test sets\n",
    "\n",
    "# Define number of layers for the ResNet neural network, select from [18, 34, 50 ,101, 152]\n",
    "num_layers = 50\n",
    "\n",
    "pretrained_weights = True\n",
    "unfreeze_all_layers = 'False' # i.e. Default: 'False', unfreezes last layer only for tuning\n",
    "\n",
    "train_augmentation = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "therapeutic-confusion",
   "metadata": {},
   "outputs": [],
   "source": [
    "bucket = None\n",
    "saved_model_path = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "intensive-warrant",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_dir = \"training_data/dogs/dog_breeds\"\n",
    "model_dir = \"models\"\n",
    "\n",
    "class _args:\n",
    "    image_height = height\n",
    "    image_width = width\n",
    "    train = os.path.join(data_dir, \"train\")\n",
    "    validation = os.path.join(data_dir, \"validation\")\n",
    "    test = os.path.join(data_dir, \"test\")\n",
    "    model_dir = model_dir\n",
    "    batch_size = batch_size\n",
    "    epochs = epochs\n",
    "    lr = lr\n",
    "    momentum = momentum\n",
    "    T_0 = T_0\n",
    "    T_mult = T_mult\n",
    "    num_layers = num_layers\n",
    "    pretrained_weights = pretrained_weights\n",
    "    s3_bucket = bucket\n",
    "    warm_restart = saved_model_path\n",
    "    unfreeze_all_layers = unfreeze_all_layers\n",
    "    train_augmentation = train_augmentation\n",
    "args = _args()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "compatible-proposition",
   "metadata": {},
   "outputs": [],
   "source": [
    "datasets = create_datasets(args)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "molecular-invite",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "----------\n",
      "train Loss: 1.3228; train Acc: 0.6826;\n",
      "validation Loss: 0.0025; validation Acc: 1.0000;\n",
      "epoch: 1; lr: 0.0033227421512487254;\n",
      "\n",
      "Epoch 2/10\n",
      "----------\n",
      "train Loss: 0.7160; train Acc: 0.8642;\n",
      "validation Loss: 0.9455; validation Acc: 0.8000;\n",
      "epoch: 2; lr: 0.008874722443520899;\n",
      "\n",
      "Epoch 3/10\n",
      "----------\n",
      "train Loss: 1.1953; train Acc: 0.7945;\n",
      "validation Loss: 0.0240; validation Acc: 0.9800;\n",
      "epoch: 3; lr: 0.0007231786991974681;\n",
      "\n",
      "Epoch 4/10\n",
      "----------\n",
      "train Loss: 0.8317; train Acc: 0.8422;\n",
      "validation Loss: 0.0002; validation Acc: 1.0000;\n",
      "epoch: 4; lr: 0.006005389605729824;\n",
      "\n",
      "Epoch 5/10\n",
      "----------\n",
      "train Loss: 0.4386; train Acc: 0.8972;\n",
      "validation Loss: 0.2304; validation Acc: 0.9400;\n",
      "epoch: 5; lr: 0.009951340343707852;\n",
      "\n",
      "Epoch 6/10\n",
      "----------\n",
      "train Loss: 1.3048; train Acc: 0.8147;\n",
      "validation Loss: 0.0000; validation Acc: 1.0000;\n",
      "epoch: 6; lr: 0.002683519824400691;\n",
      "\n",
      "Epoch 7/10\n",
      "----------\n",
      "train Loss: 0.8478; train Acc: 0.8771;\n",
      "validation Loss: 0.0000; validation Acc: 1.0000;\n",
      "epoch: 7; lr: 0.008397206521307583;\n",
      "\n",
      "Epoch 8/10\n",
      "----------\n",
      "train Loss: 0.7297; train Acc: 0.8734;\n",
      "validation Loss: 0.0000; validation Acc: 1.0000;\n",
      "epoch: 8; lr: 0.0004043233037238281;\n",
      "\n",
      "Epoch 9/10\n",
      "----------\n",
      "train Loss: 0.8255; train Acc: 0.8789;\n",
      "validation Loss: 0.0000; validation Acc: 1.0000;\n",
      "epoch: 9; lr: 0.005313952597646567;\n",
      "\n",
      "Epoch 10/10\n",
      "----------\n",
      "train Loss: 0.5141; train Acc: 0.9174;\n",
      "validation Loss: 0.0000; validation Acc: 1.0000;\n",
      "epoch: 10; lr: 0.009806308479691595;\n",
      "\n",
      "Training complete in 89m 58s\n",
      "Best validation Acc: 1.000000\n",
      "models\\model.pth\n",
      "\n",
      "Evaluating best weights:\n",
      "--------------------\n",
      "train Loss: 0.0202 Acc: 0.9927\n",
      "train Avg. F1 Score: 0.993;\n",
      "classification_report: \n",
      "                  precision    recall  f1-score   support\n",
      "\n",
      "   Border Collie       1.00      1.00      1.00       112\n",
      "       Dalmation       0.97      1.00      0.98        88\n",
      "  German Sheperd       0.99      0.99      0.99       109\n",
      "Golden Retriever       1.00      0.99      1.00       127\n",
      "       Greyhound       1.00      0.98      0.99       109\n",
      "\n",
      "        accuracy                           0.99       545\n",
      "       macro avg       0.99      0.99      0.99       545\n",
      "    weighted avg       0.99      0.99      0.99       545\n",
      ";\n",
      "\n",
      "validation Loss: 0.0025 Acc: 1.0000\n",
      "validation Avg. F1 Score: 1.000;\n",
      "classification_report: \n",
      "                  precision    recall  f1-score   support\n",
      "\n",
      "   Border Collie       1.00      1.00      1.00        10\n",
      "       Dalmation       1.00      1.00      1.00        10\n",
      "  German Sheperd       1.00      1.00      1.00        10\n",
      "Golden Retriever       1.00      1.00      1.00        10\n",
      "       Greyhound       1.00      1.00      1.00        10\n",
      "\n",
      "        accuracy                           1.00        50\n",
      "       macro avg       1.00      1.00      1.00        50\n",
      "    weighted avg       1.00      1.00      1.00        50\n",
      ";\n",
      "\n",
      "test Loss: 0.0007 Acc: 1.0000\n",
      "test Avg. F1 Score: 1.000;\n",
      "classification_report: \n",
      "                  precision    recall  f1-score   support\n",
      "\n",
      "   Border Collie       1.00      1.00      1.00        10\n",
      "       Dalmation       1.00      1.00      1.00        10\n",
      "  German Sheperd       1.00      1.00      1.00        10\n",
      "Golden Retriever       1.00      1.00      1.00        10\n",
      "       Greyhound       1.00      1.00      1.00        10\n",
      "\n",
      "        accuracy                           1.00        50\n",
      "       macro avg       1.00      1.00      1.00        50\n",
      "    weighted avg       1.00      1.00      1.00        50\n",
      ";\n",
      "\n"
     ]
    }
   ],
   "source": [
    "train(args, datasets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "southwest-engineering",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
